package com.zch.user.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.zch.user.dao.UserDao;
import com.zch.user.dao.impl.UserDaoImpl;
import com.zch.user.entity.User;
import com.zch.user.service.UserService;
import com.zch.user.util.PageUtil;
import com.zch.user.util.PasswordLock;

public class UserServiceImpl implements UserService {
	UserDao ud = new UserDaoImpl();
	@Override
	public List<User> getAllUserByPage(String uName, String rName, String tel, int pageIndex) {
		StringBuffer sb = new StringBuffer("select * from user where 1=1 ");
		List<Object> param = new ArrayList<>();
		if(uName!=null&&uName!=""){
			sb.append("and uName like ? ");
			param.add("%"+uName+"%");
		}
		if(rName!=null&&rName!=""){
			sb.append("and rName like ? ");
			param.add("%"+rName+"%");
		}
		if(tel!=null&&tel!=""){
			sb.append("and tel like ? ");
			param.add("%"+tel+"%");
		}
		sb.append("limit ?,?");
		String sql = sb.toString();
		System.out.println(sql);
		int start = (pageIndex - 1)*PageUtil.PAGE_SIZE;
		int pageSize = PageUtil.PAGE_SIZE;
		param.add(start);
		param.add(pageSize);
		Object[] params = new Object[param.size()];
		//取值并赋值
		for(int i = 0; i < params.length; i++){
			params[i] = param.get(i);
		}
		List<User> list = ud.getAllUserByPage(sql, params);
		return list;
	}

	@Override
	public int getTotalPages(String uName, String rName, String tel) {
		StringBuffer sb = new StringBuffer("select count(*) from user where 1=1 ");
		List<Object> param = new ArrayList<>();
		if(uName!=null&&uName!=""){
			sb.append("and uName like ? ");
			param.add("%"+uName+"%");
		}
		if(rName!=null&&rName!=""){
			sb.append("and rName like ? ");
			param.add("%"+rName+"%");
		}
		if(tel!=null&&tel!=""){
			sb.append("and tel like ? ");
			param.add("%"+tel+"%");
		}
		String sql = sb.toString();
		Object[] params = new Object[param.size()];
		//取值并赋值
		for(int i = 0; i < params.length; i++){
			params[i] = param.get(i);
		}
		//获得总数据量
		int counts = ud.getTotalCounts(sql, params);
		//获得总页码
		int totalPages = counts%PageUtil.PAGE_SIZE==0?
				counts/PageUtil.PAGE_SIZE:counts/PageUtil.PAGE_SIZE+1;
		return totalPages;
	}

	@Override
	public int getTotalCounts(String uName, String rName, String tel) {
		StringBuffer sb = new StringBuffer("select count(*) from user where 1=1 ");
		List<Object> param = new ArrayList<>();
		if(uName!=null&&uName!=""){
			sb.append("and uName like ? ");
			param.add("%"+uName+"%");
		}
		if(rName!=null&&rName!=""){
			sb.append("and rName like ? ");
			param.add("%"+rName+"%");
		}
		if(tel!=null&&tel!=""){
			sb.append("and tel like ? ");
			param.add("%"+tel+"%");
		}
		String sql = sb.toString();
		Object[] params = new Object[param.size()];
		//取值并赋值
		for(int i = 0; i < params.length; i++){
			params[i] = param.get(i);
		}
		//获得总数据量
		int counts = ud.getTotalCounts(sql, params);
		return counts;
	}

	@Override
	public boolean addUser(User user) {
		
		return ud.addUser(user);
	}

	@Override
	public boolean deleteUser(int uId) {
		
		return ud.deleteUser(uId);
	}

	@Override
	public boolean updateUser(User user) {
		
		return ud.updateUser(user);
	}

	@Override
	public Map<String, Object> doLogin(String uName, String password) {
		Map<String, Object> map = new HashMap<String,Object>();
		List<User> list = ud.getAllUser();
		if(list!=null){
			for(int i = 0; i < list.size(); i++){
				if(uName.equals(list.get(i).getuName())){//用户名存在
					//判断密码是否正确
					for(int j = i; j < list.size(); j++){
						if(PasswordLock.Md5Lock1(password).equals(list.get(i).getPassword())){//密码正确
							map.put("isLogin", true);//登录成功
							map.put("user", list.get(i));//存入用户对象
							return map;
						}
						if(j==list.size()-1){
							
							map.put("isLogin", false);//登录失败
							map.put("msg", "密码错误!");
							return map;
						}
					}
				}
				if(i == list.size()-1){//用户名不存在
					map.put("isLogin", false);//登录失败
					map.put("msg", "用户名不存在!");
					return map;
				}
			}
		}
		return map;
	}

	@Override
	public User getUser(int uId) {
		return ud.getUser(uId);
	}

	@Override
	public List<User> getAllUser() {
		return ud.getAllUser();
	}

}
